//
//  ST_PeculiarityDetialSupplementView.swift
//  btvideo
//
//  Created by mac on 2020/1/15.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift


class ST_PeculiarityDetialSupplementView: UICollectionReusableView {
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(cover_image_view)
        self.addSubview(bottom_view)
        
        cover_image_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(175*(ScreenWidth/375.0))
        }
        
        bottom_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(cover_image_view.snp.bottom)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(140)
        }
        
        bottom_view.addSubview(titleLabel)
        bottom_view.addSubview(content_label)
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(25)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.height.equalTo(20)
            make.width.equalTo(50)
        }
        
        bottom_view.addSubview(love_number_btn)
        love_number_btn.snp.makeConstraints { (make) in
            make.top.equalTo(content_label.snp.bottom).offset(0)
            make.centerX.equalToSuperview()
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        
        self.addSubview(click_btn)
        click_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(bottom_view.snp.bottom)
            make.height.equalTo(40)
            make.width.equalTo(200)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    fileprivate lazy var bottom_view:UIView = ST_RoundShadowView()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hex: 0x333333)
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        //        control.numberOfLines = 0
        control.textAlignment = .center
        control.cornerRadius = 10
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var love_number_btn:UIButton = {
            
         let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.white
//        control.cornerRadius = 15
//        control.borderColor = Color.init(hexString: "#ED3E58")
//        control.borderWidth = 2
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 6, bottom: 0, right: 0)
//        control.setTitleForAllStates("来一起拍视频啦")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#ED3E58")!)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_X个喜欢")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    
//    喜欢
    fileprivate lazy var selectedBackgroundImage = UIImage.init(color: Color.init(hex: 0xED3E58)!, size: CGSize.init(width: 1, height: 1))
//    添加
    fileprivate lazy var normalBackgroundImage = UIImage.init(color: Color.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1))
//    disable
    fileprivate lazy var disableBackgroundImage = UIImage.init(color: Color.init(hex: 0xE0E5EC)!, size: CGSize.init(width: 1, height: 1))
    
    
    fileprivate lazy var click_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setBackgroundImage(disableBackgroundImage, for: UIControl.State.disabled)
        
        control.setBackgroundImage(normalBackgroundImage, for: UIControl.State.normal)
        
        control.setBackgroundImage(selectedBackgroundImage, for: UIControl.State.selected)
        
        control.setTitleColorForAllStates(Color.white)
        
        control.setTitleColor(Color.init(hex: 0x61666F), for: UIControl.State.disabled)
        
        control.setTitle("添加特质", for: UIControl.State.normal)
        
        control.setTitle("喜欢特质", for: UIControl.State.selected)
        
        control.setTitle("已喜欢", for: UIControl.State.disabled)
        
        control.cornerRadius = 20
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        control.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        control.alpha = 0
        
        return control
    }()
    
//    MARK: - Bind Model
    var parent_peculiarity = ST_PeculiarityModel(){
        didSet{
            
            cover_image_view.pin_setImage(from: parent_peculiarity.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = parent_peculiarity.name
            content_label.text = parent_peculiarity.descriptionStr
            content_label.snp.updateConstraints { (make) in
                make.width.equalTo(parent_peculiarity.descriptionStr.width(withConstrainedHeight: 100, font:  UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)) + 22)
            }
            
            
            var loveStr = ""
//            特质是男
            if parent_peculiarity.type == 1{
//                自己是女
                if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex != ST_SEX.male{
//                    多少男生拥有
                    loveStr = String.init(format: "%@个男生拥有",parent_peculiarity.own_num.st_number_description())
                }
//               自己是男
                else{
//                    多少女生喜欢
                    loveStr = String.init(format: "%@个女生喜欢",parent_peculiarity.like_num.st_number_description())
                }
            }
//            特质是女
            else{
//                  自己是女
                if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex != ST_SEX.male{
//                    多少男生喜欢
                    loveStr = String.init(format: "%@个男生喜欢",parent_peculiarity.like_num.st_number_description())
                }
//               自己是男
                else{
//                    多少女生拥有
                  loveStr = String.init(format: "%@个女生拥有",parent_peculiarity.own_num.st_number_description())
                    
                }
            }
            
            love_number_btn.setTitleForAllStates(loveStr)
            
            let loveNumberBtnW = loveStr.width(withConstrainedHeight: 100, font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)) + 30
            
            love_number_btn.snp.updateConstraints { (make) in
                make.width.equalTo(loveNumberBtnW)
            }
            
            
            if parent_peculiarity.authJson["type"].intValue == 1{
//                已喜欢
                if parent_peculiarity.authJson["is_like"].boolValue{
                    click_btn.isEnabled = false
                    self.click_btn.isSelected = false
                }
//               喜欢特质
                else{
                    click_btn.isEnabled = true
                    click_btn.isSelected = true
                }
            }
//            添加特质
            else if parent_peculiarity.authJson["type"].intValue == 2{
                click_btn.isEnabled = true
                click_btn.isSelected = false
            }
        }
    }
    
    func settingClickButtonStatus(){
        
        click_btn.alpha = 1
        
    }
    
}


extension ST_PeculiarityDetialSupplementView{
    
    @objc func click_handle(sender:UIButton){
        
//        [写喜欢]点击事件
        if sender.isSelected{
            
            JVrequest(PH.likePeculiarityApi,parameters: ["feature_id":parent_peculiarity.id], success: { (json) in
                
                self.click_btn.isEnabled = false
                self.click_btn.isSelected = false
                
                
                NotificationCenter.default.post(name: Notification.Name.like_peculiarity, object: nil)
                
            }) { (code, err) in
                
                guard let view = self.parentViewController?.view else{return}
                ToastHud(text: err, view: view)
            }
        }
//        添加特质
        else{
         
            willPushPeculiarity = parent_peculiarity
            
            push_peculiarity(navigationController: self.parentViewController?.navigationController, operationController: ST_PushPeculiaritySelectMediaController())
        }
        
    }
}







fileprivate class ST_RoundShadowView: UIView {
  
    let containerView = UIView()
 
    override func layoutSubviews() {
        super.layoutSubviews()
        
      // set the shadow of the view's layer
      layer.backgroundColor = UIColor.clear.cgColor
      layer.shadowColor = UIColor.init(hexString: "#EEF2F8")!.cgColor
      layer.shadowOffset = CGSize(width: 0, height: 1.0)
      layer.shadowOpacity = 1
      layer.shadowRadius = 8
        
      // set the cornerRadius of the containerView's layer
//      containerView.roundCorners([UIRectCorner.topLeft,UIRectCorner.topRight], radius: 20)
        containerView.cornerRadius = 20
        if containerView.superview == nil{
            
            containerView.backgroundColor = Color.white
            insertSubview(containerView, at: 0)
            containerView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            
        }
    }
}



extension Int{

    /**
     数字展示规则
     */
    func st_number_description() -> String {
        
        if self > 9999{
            
            let offSetNumber = Int((Double(self) + 501)/10000.0*10)
            
            let w = Float(offSetNumber)/10.0
            
            let isInteger = floor(w) == w
            
            if !isInteger{
                return String.init(format: "%.1f", w) + "w"
            }else{
                return String.init(format: "%d", Int(w)) + "w"
            }
        }else{
            return String.init(format: "%d", self)
        }
    }
    
}
